// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const config = {
  appid: 'wx6b0daccddcc49e06', //小程序Appid，填自己的小程序id
  envName: 'cloud1-8gtv80hm04e77435', // 小程序云开发环境ID
  mchid: '1630005517', //商户号，填自己的商户号
  pfx: require('fs').readFileSync('./apiclient_cert.p12'),//这里是下载的api证书。证书怎么下在呢？网上有
  partnerKey: 'IWNSTDIAWsadwnsoSDBWAHSCGB268yvh', //此处填商户密钥
  notify_url: ' ', //支付回调网址,这里可以随意填一个网址
  spbill_create_ip: '127.0.0.1' //不用改
};

const db = cloud.database();
//const TcbRouter = require('tcb-router'); //云函数路由
const rq = require('request');
const tenpay = require('tenpay'); //支付核心模块

// 云函数入口函数
exports.main = async (event, context) => {
  let shouhuoID = event.shouhuoID
  let shouhuoPrice = event.shouhuoPrice
  const wxContext = cloud.getWXContext()
  console.log("提现走到了函数",event)
  const api = tenpay.init(config)
  // 申请企业付款到用户零钱
const orderNumber= 'dlbmoney' + new Date().getTime() + Math.floor(Math.random() * 1000)
const datas = {
  partner_trade_no: orderNumber,
  openid: shouhuoID,
  amount: shouhuoPrice * 100,
  desc: "买方已收货",
  check_name: "NO_CHECK", //不检查实名
  spbill_create_ip:"123.151.79.109"
}
const result = await api.transfers(datas)
  
return {
  event,
  openid: wxContext.OPENID,
  appid: wxContext.APPID,
  unionid: wxContext.UNIONID,
}
}